package com.ss.ttvideoengine.a;

import android.os.Handler;
import android.os.Message;

import com.ss.ttvideoengine.TTVideoEngine;
import com.ss.ttvideoengine.b.VideoDataContainer;
import com.ss.ttvideoengine.c.OkHttpRequest;
import com.ss.ttvideoengine.c.HttpRequest;
import com.ss.ttvideoengine.d.TTVideoEngineLog;
import com.ss.ttvideoengine.d.TTVideoError;
import com.ss.ttvideoengine.log.c;

import java.lang.ref.WeakReference;

public final class a {
    public static final class VideoInfoFetchCallback {
        public final WeakReference<TTVideoEngine> videoEngineWeakReference;

        public VideoInfoFetchCallback(TTVideoEngine ttVideoEngine) {
            this.videoEngineWeakReference = new WeakReference(ttVideoEngine);
        }

        public final void onFetchSuccess(VideoDataContainer videoDataContainer, TTVideoError ttVideoError) {
            TTVideoEngine ttVideoEngine = this.videoEngineWeakReference.get();
            if (ttVideoEngine != null) {
                TTVideoEngine.setVideoDataContainer(ttVideoEngine, videoDataContainer);
                if (videoDataContainer != null && ttVideoError == null) {
                    TTVideoEngineLog.log("TTVideoEngine", "fetch info success");
                    TTVideoEngine.onFetchVideoInfoSuccess(ttVideoEngine, videoDataContainer);
                    if (TTVideoEngine.getFetchVideoInfoListener(ttVideoEngine) != null) {
                        TTVideoEngine.getFetchVideoInfoListener(ttVideoEngine).onFetchedVideoInfo(videoDataContainer);
                    }
                    TTVideoEngine.onFetchedVideoInfo(ttVideoEngine, videoDataContainer);
                    return;
                }

                TTVideoEngineLog.log("TTVideoEngine", String.format("fetch info failed:%s", ttVideoError.toString()));
                TTVideoEngine.onFetchedVideoInfoFailed(ttVideoEngine, ttVideoError);
                TTVideoEngine.onVideoEngineFailed(ttVideoEngine, ttVideoError);
            }
        }

        public final void onFetchCancelled(String s) {
            TTVideoEngineLog.log("TTVideoEngine", "fetcher cancelled");
            TTVideoEngine ttVideoEngine = this.videoEngineWeakReference.get();
            if (ttVideoEngine != null) {
                TTVideoEngine.onFetchVideoInfoCancelled(ttVideoEngine, s);
                TTVideoEngine.f(ttVideoEngine);
            }
        }
    }

    final static class WeakHandler extends Handler {
        private final WeakReference<a> aRef;

        public WeakHandler(a arg2) {
            this.aRef = new WeakReference(arg2);
        }

        public final void handleMessage(Message message) {
            a v0 = this.aRef.get();
            if (v0 != null) {
                VideoInfoFetchCallback v1 = v0.e;
                if (v1 != null) {
                    if (v0.b) {
                        v1.onFetchCancelled("fetcher is cancelled");
                    } else {
                        switch (message.what) {
                            case 0: {
                                TTVideoError ttVideoError = (TTVideoError) message.obj;
                                if (ttVideoError != null) {
                                    TTVideoEngineLog.log("TTVideoEngine", String.format("fetcher should retry, error:%s", ttVideoError.toString()));
                                    TTVideoEngine ttVideoEngine = v1.videoEngineWeakReference.get();
                                    if (ttVideoEngine != null) {
                                        TTVideoEngine.c(ttVideoEngine).e.add(ttVideoError);
                                        return;
                                    }
                                }
                            }
                            case 1: {
                                v1.onFetchSuccess(null, (TTVideoError) message.obj);
                                return;
                            }
                            case 2: {
                                v1.onFetchSuccess((VideoDataContainer) message.obj, ((TTVideoError) null));
                                return;
                            }
                            case 3: {
                                int v2_1 = message.arg1;
                                TTVideoEngine ttVideoEngine = v1.videoEngineWeakReference.get();
                                if (ttVideoEngine != null) {
                                    TTVideoEngineLog.log("TTVideoEngine", String.format("video status exception:%d", Integer.valueOf(v2_1)));
                                    if (TTVideoEngine.c(ttVideoEngine) != null) {
                                        c v1_2 = TTVideoEngine.c(ttVideoEngine);
                                        if (v1_2.c != null) {
                                            v1_2.c.vsc = v2_1;
                                            v1_2.b();
                                        }
                                    }
                                    if (TTVideoEngine.getVideoListener(ttVideoEngine) != null) {
                                        TTVideoEngine.getVideoListener(ttVideoEngine).onVideoStatusException(v2_1);
                                    }
                                }
                            }
                        }
                        return;

                    }
                }
            }
        }
    }

    public Handler a;
    public boolean b;
    public int c;
    public String d;
    public VideoInfoFetchCallback e;
    public HttpRequest f;

    public a(HttpRequest arg2) {
        this.b = false;
        this.c = 0;
        this.a = new WeakHandler(this);
        this.f = arg2 == null ? new OkHttpRequest() : arg2;
    }

    public final void a() {
        this.f.request(this.d, new com.ss.ttvideoengine.a.b(this));
    }

    final void a(TTVideoError arg3) {
        this.a.sendMessage(this.a.obtainMessage(1, arg3));
    }
}

